﻿using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Events;

using Might.Infrastructure.DOM;
using Might.Infrastructure.Events;
using Might.Infrastructure.Services;

namespace Might.UIControls.StatusBar {
    public class StatusBarPresenter {
        private IDom domRoot;

        public StatusBarPresenter() {
            CoreServices.EventAggregator.GetEvent<ProgressEvent>().Subscribe(ProgressEventsHandler, ThreadOption.UIThread, false);
            domRoot = CoreServices.Container.Resolve<IDom>();

            domRoot.LoginFinished += new EndProgressEventHandler( domRoot_LoginFinished );
            domRoot.LogoutFinished += new EndProgressEventHandler( domRoot_LogoutFinished ); 
        }

        void domRoot_LoginFinished( IProgressToken sender , EndProgressEventArgs e ) {
            if( e.Result == ProgressResult.Success ) {
                View.UserName = ( domRoot.GetFirstChild( GroupNames.User ) as IUserInfo ).Name;
            }
        }

        void domRoot_LogoutFinished( IProgressToken sender , EndProgressEventArgs e ) {
            View.UserName = "";
        }   

        public IStatusBarView View { get; set; }

        public void ProgressEventsHandler(IProgressToken progressToken) {
            View.AddProgress(progressToken);
        }
        
    }
}
